package com.web.mall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.mall.pojo.*;
import com.web.mall.service.ContentCateManagerService;
import com.web.mall.service.ContentManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@ResponseBody
//@Component
public class ContentController {
    @Autowired
    private ContentManagerService contentManagerService;

    @Autowired
    private ContentCateManagerService contentCateManagerService;

    @GetMapping("/ContentListByPage")
    public PageResp<TbContent> listByPage(PageReq pageReq){
        PageHelper.startPage(pageReq.getPage(),pageReq.getSize());
        Page<TbContent> page = (Page<TbContent>) contentManagerService.queryContent();

        PageResp pageResp = new PageResp();
        pageResp.setTotal(page.getTotal());
        pageResp.setList(page.getResult());

        return pageResp;
    }

    @GetMapping("/findAllContentCategory")
    public List<TbContentCategory> findAllContentCategory(){
        return contentCateManagerService.findContentCategoryList();
    }

    @DeleteMapping("/deleteContent/{id}")
    public Result deleteContent(@PathVariable Long id){
        boolean b= this.contentManagerService.deleteContent(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/saveContent")
    public Result saveContent(@RequestBody TbContent content){
        System.out.println(content.getId());
        boolean b= this.contentManagerService.saveContent(content);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @GetMapping("/findByCategoryId/{categoryId}")
    public List<TbContent> findByCategoryId(@PathVariable Long categoryId){
        return contentManagerService.findByCategoryId(categoryId);
    }
}
